package com.example.recyclerviewdemo

import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * 填充RecyclerView Adapter状态，空页面或网络异常或其他错误
 * [statusResId] 代表状态的资源图片
 * [hintText] 提示文字
 * [retryAction] 点击重试按钮触发的行为
 */
fun <T> BaseQuickAdapter<T, BaseViewHolder>.fillStatus(
    statusResId: Int = R.drawable.hint_empty,
    hintText: String = "当前暂无数据",
    retryAction: (() -> Unit)? = null,
) {
    LayoutInflater.from(context).inflate(R.layout.rv_empty, recyclerView, false).apply {
        findViewById<AppCompatImageView>(R.id.iv_error).setImageResource(statusResId)
        findViewById<AppCompatTextView>(R.id.tv_error_hint).text = hintText
        findViewById<AppCompatButton>(R.id.btn_retry).apply {
            if (retryAction == null) visibility = View.GONE else visibility = View.VISIBLE
            setOnClickListener {
                retryAction?.invoke()
            }
        }
        setEmptyView(this)
    }
}